﻿using Infrastructure.Dao;
using SqlSugar;
using SyntacticSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels.Activity;

namespace DormMS.Service
{
    public class ActivityRuleService : IActivityRuleService
    {

        private SqlSugarClient db;
        public IEnumerable<ActivityRule> GetActivityRuleList(ActivityRuleSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                var list = db.Queryable<ActivityRule>()
                    .Where(m => m.RuleContent.Contains(search.Keywords))
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total).ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取规则信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActivityRule GetActivityRuleInfo(int Id)
        {
            ActivityRule activityrule = null;
            using (db = DbConfig.GetInstance())
            {
                string sql =
                    $@"select * from ActivityRule  where Id={
                        Id}";
                var dt = db.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    activityrule = dt.TryToList<ActivityRule>()[0];
                }
                return activityrule;
            }
        }

        /// <summary>
        /// 修改规则
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditActivityRule(Model.ActivityRule model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.ActivityRule>(model);
            }
        }

        /// <summary>
        /// 增加规则
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddActivityRule(Model.ActivityRule model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Insert<Model.ActivityRule>(model).ObjToInt();
            }
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DelActivityRule(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.ActivityRule>(new { IsDeleted = true }, m => m.ID == Id);
            }
        }


        /// <summary>
        /// 获取所有规则
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<ActivityRule> GetActivityRuleAll(int type)
        {
            List<ActivityRule> activityrule = null;
            using (db = DbConfig.GetInstance())
            {
                string sql =
                    $@"select * from ActivityRule where RuleType={
                        type} and isdeleted=0";
                var dt = db.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    activityrule = dt.TryToList<ActivityRule>();
                }
                return activityrule;
            }
        }


    }


}
